<?php

/**
 * @todo:京东有些商品价格从分享中找出来不准，还需要进一步修改
 */
class Uuch_Reader_CommonReader extends Uuch_Reader_ProductReader {
    
    public function getInfo($url) {
        $client = new Zend_Http_Client();
        $client->setEncType('');
        $client->setUri($url);
        $response = $client->request();
        $body = mb_convert_encoding($response->getBody(),'utf-8','gbk');
        
        $uri = parse_url($url);
        $name = explode('.',$uri['host']);
        $reader = ucfirst(strtolower($name[count($name)-2]));
//        $config = Application_Model_ReaderConfig::getBySite($reader);
        $config = new Application_Model_ReaderConfig();
        $config->ProductNameRegex = '#(<div class="name_info">([\\W\\w.]+?)<\\/div>|<h1>([\\W\\w.]+?)<span class="subtitle">)#i';
        $config->PriceRegex = '#(<span class="price_d">&yen;(.+?)<\\/span>|id="promo_price" prpr="(.+?)"|class="yen">&yen;<\/span>|id="salePriceTag" ddsp="(.+?)")#i';
        $config->SkuRegex = '#/(\w+).html#im';
        $config->ImageRegex = '#wsrc="(.+?)"#im';
        $config->ShopRegex = '';
        //$titleReg = '#<h1>([\W\w.]+?)<#i';
        $product = new Application_Model_Product();
        $matches = array();
        if(preg_match($config->ProductNameRegex, $body,$matches)){
           // $product->Name = $matches[1];
            if(strtolower($reader) == "dangdang") {
                $product->Name = strip_tags(trim(end($matches)));
            } else {
                $product->Name = preg_replace("/\<span(.*)\>.+?.\<\/span\>/",'', $matches[1]);
                $product->Name = trim($product->Name);
            }
        }
        //$priceReg = '#京东价：￥([\W\w.]+?)。#i';
        if(preg_match($config->PriceRegex, $body,$matches)){
            if(strtolower($reader) == "dangdang") {
                $product->Price = (float)end($matches);
            } else {
                $product->Price = (float)$matches[2];
            }
        }        
        
        $id = $url;
        if(preg_match($config->SkuRegex, $url,$matches)){
            $id= $matches[1];
        }
        
        $product->Sku = $id;
        $product->Url = $url;
        
        
        $product->ShipFee = 0;
        $product->Quantity = 998;
        
        //$imageReg = '#<img onerror="this.src=\'http://www.360buy.com/images/none/none_347.gif\'"[\s]+?src="(.+?)" width#im';
        if(preg_match($config->ImageRegex, $body,$matches)){
            $product->Image = $matches[1];
            $product->RemoteImage = $product->Image;
        }
        
        $shopName = $reader;
        if($config->ShopRegex && preg_match($config->ShopRegex, $body,$matches)){
            $shopName = $matches[1];
        }
        
       $shop = Application_Model_Shop::getByName($shopName);
       if (NULL == $shop){
           $shop = new Application_Model_Shop();
           $shop->Name = $shopName;
           $shop->IsTaobao = True;
           $shop->Url = $uri['host'];
           $shop->save();
       }
       $product->ShopId = $shop->Id;
       $product->save();
//            print_r($product);exit;
        return $product;
        
    }
    
    public function getInfoForNew($url) {
        $client = new Zend_Http_Client();
        $client->setEncType('');
        $client->setUri($url);
        $response = $client->request();
        $body = mb_convert_encoding($response->getBody(),'utf-8','gbk');
        echo 'location:';
        echo $response->getHeader('location');
        echo 'url:';
        echo   $url;
        exit;
        $uri = parse_url($url);
        $name = explode('.',$uri['host']);
        $reader = ucfirst(strtolower($name[count($name)-2]));
        $config = Application_Model_ReaderConfig::getBySite($reader);
        
        //$titleReg = '#<h1>([\W\w.]+?)<#i';
        
        $product = new Application_Model_Product();
        $matches = array();
        if(preg_match($config->ProductNameRegex, $body,$matches)){
           // $product->Name = $matches[1];
            if(strtolower($reader) == "dangdang") {
                $product->Name = strip_tags(trim(end($matches)));
            } else {
                $product->Name = preg_replace("/\<span(.*)\>.+?.\<\/span\>/",'', $matches[1]);
                $product->Name = trim($product->Name);
            }
        }
        
        //$priceReg = '#京东价：￥([\W\w.]+?)。#i';
        if(preg_match($config->PriceRegex, $body,$matches)){
            if(strtolower($reader) == "dangdang") {
                $product->Price = (float)end($matches);
            } else {
                $product->Price = (float)$matches[2];
            }
        }
        
        
        
        $id = $url;
        if(preg_match($config->SkuRegex, $url,$matches)){
            $id= $matches[1];
        }
        
        
        $product->Sku = $id;
        $product->Url = $url;
        
        
        $product->ShipFee = 0;
        $product->Quantity = 997;
        
        //$imageReg = '#<img onerror="this.src=\'http://www.360buy.com/images/none/none_347.gif\'"[\s]+?src="(.+?)" width#im';
        if(preg_match($config->ImageRegex, $body,$matches)){
            $product->Image = $matches[1];
            $product->RemoteImage = $product->Image;
        }
        //#data-tracelog="wp_widget_supplierinfo_compname"><span class="info ">([\w\W]+?)<#im
        $shopName = $reader;
        if($config->ShopRegex && preg_match($config->ShopRegex, $body,$matches)){
            $shopName = $matches[1];
        }
        $shop = Application_Model_Shop::getByName($shopName);
        if (NULL == $shop){
            $shop = new Application_Model_Shop();
            $shop->Name = $shopName;
            $shop->IsTaobao = True;
            $shop->Url = $uri['host'];
            $shop->save();
        }
        $product->ShopId = $shop->Id;
        
        $product->save();
        //return $product;
        
        return array(
            'product' => $product, 
            'shopName' => strip_tags($shopName),
            'siteName' => strpos($url,'1688.com') === false ? '当当' : '阿里巴巴',
            'shopUrl' => $url,
            'url' => $url,
            'siteUrl' => $uri['scheme'] . '://' . $uri['host']);
        
    }
}
